<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="访客计划名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="客户的访客计划" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="visitorNameArr">
              <a-select
                mode="multiple"
                v-model="model.visitorNameArr"
                style="width: 100%"
                placeholder="请选择客户名称"
                disabled
              >
              </a-select>
            </a-form-model-item>
          </a-col>

          <!--          <a-col :span="24">
                      <a-form-model-item label="执行人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="executor">
                        <j-checkbox
                          v-model="model.executor"
                          :options="jCheckboxOptions"
                          @change="zhixingrenChange"
                        />
                      </a-form-model-item>
                    </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="执行人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="executor">
              <JSelectUserByDepCustom v-model="model.executor" :multi="true" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="拜访目的" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purposeVisit">
              <j-search-select-tag v-model="model.purposeVisit" dict="vistor_program_objcetive" />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="计划访问时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="implementTime">
              <a-date-picker
                style="width: 100%"
                placeholder="请选择跟进时间"
                :show-time="true"
                v-model="model.implementTime"
                format="YYYY-MM-DD HH:mm:ss"
                :disabled-date="disabledDate"
                @change="priceExamineTimeChange"
              />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="提醒" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remind">
              <j-search-select-tag v-model="model.remind" dict="vistor_program_remind" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import JSelectMultiUser from '@/components/jeecgbiz/JSelectMultiUserCustom'
import JCheckbox from '@/components/jeecg/JCheckbox'
import JSelectUserByDepCustom from '@/components/jeecgbiz/JSelectUserByDepCustom'
import moment from 'moment'

export default {
  name: 'VisitorProgramForm',
  components: {
    JSelectMultiUser,
    JCheckbox,
    JSelectUserByDepCustom
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  watch: {
    addVisitor: {
      deep: true,
      handler(newValue, oldValue) {
        if (newValue) {
          this.model.addVisitor = 1
        } else {
          this.model.addVisitor = 0
        }
      }
    }
  },
  data() {
    return {
      addVisitor: false,
      jCheckboxOptions: [
        { label: '负责人', value: '1' },
        { label: '协同人', value: '2' },
        { label: '自定义', value: '3' }
      ],
      customCustomerIdShow: false,
      customfollowTime2Show: false,
      model: {
        addVisitor: 0
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        name: [
          { required: true, message: '请输入计划名!' }
        ],
        customerId: [
          { required: true, message: '请输入客户!' }
        ],
        executor: [
          { required: true, message: '执行人不能为空!' }
        ],
        implementTime: [
          { required: true, message: '计划访问时间不能为空!' },
          {
            validator: (rule, value, callback) => {
              console.log('value', value)
              let NendTime = value.replace(new RegExp('-', 'g'), '/')
              if (new Date().getTime() < new Date(NendTime).getTime()) {
                callback()
              } else {
                callback(false)
              }

            },
            message: '跟进时间必须大于当前时间'
          }
        ],
        remind: [
          { required: false, message: '提醒时间不能为空!' },
          {
            validator: (rule, value, callback) => {
              //表单上面填的提醒时间
              let tixingTime = null
              //系统当前时间
              let nowTime = new Date().getTime()

              if (this.model.implementTime) {
                tixingTime = this.model.implementTime.replace(new RegExp('-', 'g'), '/')
              } else {
                callback(new Error('请先选择执行时间'))
              }

              switch (value) {
                case '1':
                  let m15 = 15 * 60 * 1000
                  if ((new Date(tixingTime).getTime() - m15) > nowTime) {
                    callback()
                  } else {
                    callback(new Error('提醒时间不能早于执行时间'))
                  }
                  break
                case '2':
                  let h1 = 60 * 60 * 1000
                  if ((new Date(tixingTime).getTime() - h1) > nowTime) {
                    callback()
                  } else {
                    callback(new Error('提醒时间不能早于执行时间'))
                  }
                  break
                case '3':
                  let d1 = 24 * 60 * 60 * 1000
                  if ((new Date(tixingTime).getTime() - d1) > nowTime) {
                    callback()
                  } else {
                    callback(new Error('提醒时间不能早于执行时间'))
                  }
                  break
                case '4':
                  let w1 = 7 * 24 * 60 * 60 * 1000
                  if ((new Date(tixingTime).getTime() - w1) > nowTime) {
                    callback()
                  } else {
                    callback(new Error('提醒时间不能早于执行时间'))
                  }
                  break
                default:
                  callback()
                  break
              }
            },
            trigger: ['change']
          }
        ]
      },
      url: {
        add: '/visitorprogram/visitorProgram/add',
        edit: '/visitorprogram/visitorProgram/edit',
        queryById: '/visitorprogram/visitorProgram/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    switchOnChange(e) {
      console.log(e)
    },
    priceExamineTimeChange(e, e1) {
      this.model.implementTime = e1
    },
    priceExamineTimeChange2(e, e1) {
      this.model.customTime = e1
    },
    disabledDate(current) {
      return current && current < moment().subtract(1, 'days').endOf('day')
    },
    customFloowTime2Change(e) {
      if (e == 0) {
        this.customfollowTime2Show = true
      } else {
        this.customfollowTime2Show = false

      }
    },
    add(record) {
      this.edit(record)
      console.log('record', record)
      // this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.model.name = record.visitorNameArr[0] + '等' + record.visitorNameArr.length + '个客户的访问计划'
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    //执行人
    zhixingrenChange(e) {
      // customCustomerIdShow
      console.log('e', typeof e)
      let arr = e.split(',')
      console.log('arr', arr)
      let customCustomerIdShow = arr.some(res => {
        return res == e
      })
      if (customCustomerIdShow) {
        this.customCustomerIdShow = true
      } else {
        this.customCustomerIdShow = false

      }
    }
  }
}
</script>